package hxy.cupb.entity;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.security.core.userdetails.UserDetails;

@Slf4j
//@Accessors(chain = true)
@Data
public class UserEntity implements UserDetails, Serializable {
    // 主键
    private int id;
    private String userName;
    private String passWord;
//    private int age;
    // tinyint(1)
    private Boolean enabled;
    // tinyint(1)
    private Boolean locked;
    private List<RoleEntity> roles;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        List<SimpleGrantedAuthority> authorities = new ArrayList<> ();
        log.info("{}",roles);
        for (RoleEntity role : roles){
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
//        return roles;
    }
    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public String getPassword() {
        return passWord;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }


}
